package cn.xiaou.view;



import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import cn.xiaou.domain.User;
import cn.xiaou.draw.MyJButtom;
import cn.xiaou.draw.MyTextFieldBorder;
import cn.xiaou.service.UserService;
import cn.xiaou.service.impl.UserServiceImpl;
import cn.xiaou.utils.ImageManagerUtils;


/*
 * 功能：验证邮箱，登录功能
 * 创建日期:2018-06-16 04:12:16
 * 最后修改时间：2018-06-17 20:21:56
 * 
 */
public class LoginView extends JFrame{
	private static final long serialVersionUID = 1L;
	private UserService userservice = new UserServiceImpl();
	@SuppressWarnings("unused")
	private ImageIcon buttonBg;
	public LoginView() {
//		buttonBg=new ImageIcon(ImageManagerUtils.button3);
		setIconImage(getToolkit().getImage(ImageManagerUtils.icoUrl));
		this.setTitle("登录");
		this.setSize(420,230);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.addMouseMotionListener(new JFrameMove(this));
		
		//------------------------------------------------------------------
		 ImageIcon background = new ImageIcon(ImageManagerUtils.loginView);
		 JLabel bg = new JLabel(background);
		 bg.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
		 JPanel panel = (JPanel) getContentPane();  
		 //设置透明
		 panel.setOpaque(false);
		 //将背景图的label置于窗体的底层
		 getLayeredPane().add(bg, new Integer(Integer.MIN_VALUE));  
		 panel.setLayout(null);
		//------------------------------------------------------------------
		/*Toolkit toolkit = this.getToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		this.setLocation((screenSize.width-getWidth())/2, (screenSize.height-getHeight())/2);*/
		setLocationRelativeTo(null);
		setLayout(null);
		panel.setSize(new Dimension(500, 350));
		JLabel nameLabel = new JLabel("用户名：");
		nameLabel.setBounds(60, 60, 100, 25);
		panel.add(nameLabel);
		nameLabel.setFont(new Font("黑体", Font.BOLD, 20));
		
		JLabel passwordLabel = new JLabel("密  码：");
		passwordLabel.setBounds(60, 95, 100, 25);
		panel.add(passwordLabel);
		passwordLabel.setFont(new Font("黑体", Font.BOLD, 20));
		/*用户名*/
		JTextField username = new JTextField();
		username.setBounds(140, 60, 150, 25);
		username.addFocusListener(new ShineJText(Color.WHITE));
		panel.add(username);
		username.setOpaque(false);
		username.setBorder(new MyTextFieldBorder());
		/*密码*/
		JPasswordField password = new JPasswordField();
		panel.add(password);
		password.setBounds(140, 95, 150, 25);
		password.addFocusListener(new ShineJText(Color.WHITE, Color.BLACK));
		password.setOpaque(false);
		password.setBorder(new MyTextFieldBorder());
		
		//
		JButton b1 = new MyJButtom(ImageManagerUtils.b1);
		panel.add(b1);
		b1.setBounds(90, 145, 100, 25);
		/*ImageIcon b1p=new ImageIcon(ImageManagerUtils.b1);
		b1.setIcon(b1p);
		b1.setMargin(new Insets(0, 15, 0, 0));  */
		b1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//JOptionPane.showInternalInputDialog(button2,"电子邮箱激活码"); 返回String
				if(!(username.getText().length()>0 && new String(password.getPassword()).length()>0)) {
					//JOptionPane.showConfirmDialog(frame, "用户名或密码错误", "提示窗口",JOptionPane.WARNING_MESSAGE);
					Object[] options = { "返回"}; 
					JOptionPane.showOptionDialog(null, "密码或用户名为空", "警告", 
							JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
							null, options,options[0]); 
					return;
				}
				User user = userservice.loginUser(username.getText(),new String(password.getPassword()));
				if(user != null) {
					if(!"0".equals(user.getEmail_state())) {
						String code =JOptionPane.showInputDialog("请输入电子邮箱激活码");
						if(code==null) {
							Object[] options = { "确定"}; 
							JOptionPane.showOptionDialog(null, "未输入电子邮件激活码", "提示", 
									JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, 
									null, options,options[0]); 
						}
						if(user.getEmail_state().equals(code) && code!=null) {
							userservice.activationMailbox(user.getUid());
							/*Object[] options = { "点击进入"}; 
							JOptionPane.showOptionDialog(null, "登录成功", "提示", 
									JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, 
									null, options,options[0]); */
							//登录成功后
							//-------------------------
							new MainFrame(user);
							dispose();
						}else if(code !=null){
							//JOptionPane.showConfirmDialog(null, "激活码错误", "提示窗口",JOptionPane.WARNING_MESSAGE);
							Object[] options = { "返回"}; 
							JOptionPane.showOptionDialog(null, "激活码错误", "警告", 
									JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
									null, options,options[0]); 
						}
					}else {
						/*Object[] options = { "点击进入"}; 
						JOptionPane.showOptionDialog(null, "登录成功", "提示", 
								JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, 
								null, options,options[0]);*/
						//登录成功后
						//-------------------------
						new MainFrame(user);
						dispose();
					}
				}else {
					//JOptionPane.showConfirmDialog(frame, "用户名或密码错误", "提示窗口",JOptionPane.WARNING_MESSAGE);
					Object[] options = { "返回"}; 
					JOptionPane.showOptionDialog(null, "用户名或密码错误", "警告", 
							JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
							null, options,options[0]); 
				}
				
			}
		});
		//
		JButton b2 = new MyJButtom(ImageManagerUtils.b2);
		panel.add(b2);
		b2.setBounds(250, 145, 100, 25);
//		ImageIcon b2p=new ImageIcon(ImageManagerUtils.b2);
//		b2.setIcon(b2p);
//		b2.setMargin(new Insets(0, 15, 0, 0)); 
		b2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText("");
				password.setText("");
			}
		});
		JButton b3 = new MyJButtom(ImageManagerUtils.b3);
		panel.add(b3);
		b3.setBounds(310, 60, 100, 25);
//		ImageIcon b3p=new ImageIcon(ImageManagerUtils.b3);
//		b3.setIcon(b3p);
//		b3.setMargin(new Insets(0, 15, 0, 0)); 
		b3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new RegisterView();
				setVisible(false);
			}
		});
		JButton b4 = new MyJButtom(ImageManagerUtils.b4);
		panel.add(b4);
		b4.setBounds(310, 95, 100, 25);
//		ImageIcon b4p=new ImageIcon(ImageManagerUtils.b4);
//		b4.setIcon(b4p);
//		b4.setMargin(new Insets(0, 15, 0, 0)); 
		b4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				new ForgetPasswordView();
				setVisible(false);
			}
		});
		
		JButton b5 = new MyJButtom(ImageManagerUtils.b5);
		panel.add(b5);
		b5.setBounds(370, 180, 50, 50);
//		ImageIcon b5p=new ImageIcon(ImageManagerUtils.b5);
//		b5.setIcon(b5p);
//		b5.setMargin(new Insets(0, 15, 0, 0)); 
		b5.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
				
			}
		});
		
		setUndecorated(true);
	    setBackground(new Color(0,0,0,0));
		
		this.setVisible(true);
	}
//	public static void main(String[] args) {
//		new LoginView();
//	}

}
